import requests

def addVnf(vim_ip:str, vim_port:int, vnf_name:str, vnf_type:str, vnf_ip:str, vnf_port:int,
           vnf_cpu:float, vnf_ram:float, vnf_rom:float):
    vim_url = f'http://{vim_ip}:{vim_port}/add_vnf'

    data = {
        'vnfname': vnf_name,
        'vnftype': vnf_type,
        'vnfip': vnf_ip,
        'vnfport': vnf_port,
        'vnfcpu': vnf_cpu,
        'vnfram': vnf_ram,
        'vnfrom': vnf_rom,
        'is_program_request': "true"
    }

    response = requests.post(vim_url, data=data)

    if response.status_code == 201:
        print("VNF added successfully")
    else:
        error_message = response.json().get('message', 'Unknown error')
        print(f"Failed to add VNF: {response.status_code} for {error_message}")

def deleteVnf(vim_ip:str, vim_port:int, vnf_name:str):
    vim_url = f'http://{vim_ip}:{vim_port}/delete_vnf/{vnf_name}'

    data = {
        'is_program_request': "true"
    }

    response = requests.post(vim_url, data=data)

    if response.status_code == 200:
        print("VNF deleted successfully")
    else:
        error_message = response.json().get('message', 'Unknown error')
        print(f"Failed to delete VNF: {response.status_code} for {error_message}")

def getRemain(vim_ip:str, vim_port:int):
    vim_url = f'http://{vim_ip}:{vim_port}/get_resources'

    response = requests.get(vim_url)

    if response.status_code == 200:
        cpu_remain = response.json().get('cpu_remain', 'Unknown error')
        ram_remain = response.json().get('ram_remain', 'Unknown error')
        print(f"Get info from vim:///{vim_ip}:{vim_port}:")
        print(f" | remain cpu: {cpu_remain} MHz | remain ram: {ram_remain} GB | ")
    else:
        print(f"Failed to get info from vim:///{vim_ip}:{vim_port}")

def getVnfs(vim_ip:str, vim_port:int):
    vim_url = f'http://{vim_ip}:{vim_port}/get_vnfs'

    response = requests.get(vim_url)

    if response.status_code == 200:
        vnf_names = response.json().get('vnf_names', [])
        print(f"Get info from vim:///{vim_ip}:{vim_port}:")
        print(f" | deployed vnfs: {vnf_names}| ")
    else:
        print(f"Failed to get info from vim:///{vim_ip}:{vim_port}")


if __name__ == "__main__":
    vim_ip:str = '192.168.31.80'
    vim_port:int = 5000

    addVnf(vim_ip=vim_ip, vim_port=vim_port, vnf_name='vnf_1', vnf_type='matinv',
           vnf_ip=vim_ip, vnf_port=5001, vnf_cpu=0.5, vnf_ram=0.2, vnf_rom=10)
    
    addVnf(vim_ip=vim_ip, vim_port=vim_port, vnf_name='vnf_2', vnf_type='matprint',
           vnf_ip=vim_ip, vnf_port=5002, vnf_cpu=0.2, vnf_ram=0.1, vnf_rom=20)

    input()
    addVnf(vim_ip=vim_ip, vim_port=vim_port, vnf_name='vnf_2', vnf_type='matprint',
           vnf_ip=vim_ip, vnf_port=5002, vnf_cpu=0.2, vnf_ram=0.1, vnf_rom=20)
    getRemain(vim_ip=vim_ip, vim_port=vim_port)
    getVnfs(vim_ip=vim_ip, vim_port=vim_port)

    input()
    addVnf(vim_ip=vim_ip, vim_port=vim_port, vnf_name='vnf_3', vnf_type='matprint',
           vnf_ip=vim_ip, vnf_port=5003, vnf_cpu=1, vnf_ram=0.1, vnf_rom=20)
    getRemain(vim_ip=vim_ip, vim_port=vim_port)
    getVnfs(vim_ip=vim_ip, vim_port=vim_port)

    input()
    deleteVnf(vim_ip=vim_ip, vim_port=vim_port, vnf_name='vnf_1')
    getRemain(vim_ip=vim_ip, vim_port=vim_port)
    getVnfs(vim_ip=vim_ip, vim_port=vim_port)

    input()
    deleteVnf(vim_ip=vim_ip, vim_port=vim_port, vnf_name='vnf_4')
    getRemain(vim_ip=vim_ip, vim_port=vim_port)
    getVnfs(vim_ip=vim_ip, vim_port=vim_port)
    



